﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Service.SystemManage;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.SystemManage
{
    public class OpLinesBusiness
    {
        private readonly OpLinesService _dal = new OpLinesService();
        public string GetLineListById(OpLinesQcEntity qcmodel)
        {
            var total = _dal.GetLineListByIdCount(qcmodel);
            var list = _dal.GetLineListById(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetOpLinesList(OpLinesQcEntity qcmodel)
        {
            var total = _dal.GetOpLinesCount(qcmodel);
            var list = _dal.GetOpLinesList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public bool AddOpLines(string data)
        {
            var result = false;
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<OpLinesEntity>();
            var userList = new ArrayList();
            var lineList = new ArrayList();
            foreach (Hashtable row in rows)
            {
                userList = (ArrayList)row["userList"];
                lineList = (ArrayList)row["lineList"];
            }
            foreach (var i in userList)
            {
                list.AddRange(from object j in lineList
                              select new OpLinesEntity
                                         {
                                             RowID = Guid.NewGuid(), UserID = new Guid(i.ToString()), LineID = new Guid(j.ToString())
                                         });
            }

            if (list.Count > 0)
            {
                result = _dal.AddOpLines(list);
            }
            return result;
        }

        public bool RemoveOpLinesByUserID(string id)
        {
            return _dal.RemoveOpLinesByUserID(id);
        }

        public bool RemoveOpLinesByRowID(string id)
        {
            return _dal.RemoveOpLinesByRowID(id);
        }
    }
}
